<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/29 10:28
 * Remark:
 */

namespace App\ServiceChecker\Company;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class CompanyLegitimacy extends BasicServiceChecker
{

    public function verifyCompanyCodeIsExists($companyCode){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_code' => $companyCode,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '公司编码已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }


    public function verifyCompanyEdit($companyName,$companyId){
        $setNames = $this->_companyModel->setGetEditCheckerNames('company_name',$companyName);
        $setIds = $this->_companyModel->setGetEditCheckerIds('company_id',$companyId);
        $setCondition = $this->_companyModel->setGetEditCheckerCondition('status',BaseConsts::STATUS_NORMAL);
        $isExists = $this->_companyModel->editNameChecker(
            $setNames,
            $setIds,
            [
                $setCondition
            ]
        );
        if($isExists){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '公司名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 校验公司名称是否存在
     * @param $companyName
     */
    public function verifyCompanyNameIsExists($companyName){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_name' => $companyName,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '公司名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 修改时检验公司名称是否存在
     * @param $companyId
     * @param $companyName
     */
    public function verifyCompanyNameIsExistsByEdit($companyId, $companyName){
        $editIds = $this->_companyModel->setGetEditCheckerIds('company_id',$companyId);
        $editNames = $this->_companyModel->setGetEditCheckerNames('company_name',$companyName);
        $isExists = $this->_companyModel->editNameChecker($editNames,$editIds);
        if($isExists){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前公司名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }


    /**
     * 是否存在子节点
     * @param $companyId
     * @param $msg
     */
    public function verifyCompanyHasChildIsExists($companyId){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_parent_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '存在子节点不能操作',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyCompanyParentIsLegal($companyId,$parentId){
        // 校验父公司不能为自己
        if($companyId == $parentId){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '父公司不能为自己',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        // 校验当前公司是否存在子节点
        $isExist = $this->_companyModel->isExistByCondition([
            'company_parent_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '当前公司存在子节点不能修改',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        // 校验当前父公司是否存在
        $isExist = $this->_companyModel->isExistByCondition([
            'company_id' => $parentId,
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '父公司不存在或已删除',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}